package com.example.hello.service.impl;

import com.example.hello.dto.AdvertisementDTO;
import com.example.hello.entity.Advertisement;
import com.example.hello.mapper.AdvertisementMapper;
import com.example.hello.service.AdvertisementService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Calendar;

@Service
public class AdvertisementServiceImpl implements AdvertisementService {

    @Autowired
    private AdvertisementMapper advertisementMapper;

    @Override
    @Transactional
    public Advertisement createAdvertisement(AdvertisementDTO advertisementDTO, Long userId) {
        Advertisement advertisement = new Advertisement();
        BeanUtils.copyProperties(advertisementDTO, advertisement);
        advertisement.setCreatedBy(userId);
        
        // 只保存时间部分，不包含年月日
        // 使用 SQL TIME 类型存储，这样在数据库中只会保存时间部分
        if (advertisement.getStartTime() != null) {
            Calendar cal = Calendar.getInstance();
            cal.setTime(advertisement.getStartTime());
            
            // 创建一个只包含时间部分的 java.sql.Time 对象
            java.sql.Time sqlTime = new java.sql.Time(
                cal.get(Calendar.HOUR_OF_DAY), 
                cal.get(Calendar.MINUTE), 
                cal.get(Calendar.SECOND)
            );
            advertisement.setStartTime(sqlTime);
        }
        
        if (advertisement.getEndTime() != null) {
            Calendar cal = Calendar.getInstance();
            cal.setTime(advertisement.getEndTime());
            
            // 创建一个只包含时间部分的 java.sql.Time 对象
            java.sql.Time sqlTime = new java.sql.Time(
                cal.get(Calendar.HOUR_OF_DAY), 
                cal.get(Calendar.MINUTE), 
                cal.get(Calendar.SECOND)
            );
            advertisement.setEndTime(sqlTime);
        }
        
        // 插入广告
        advertisementMapper.insertAdvertisement(advertisement);
        
        // 返回创建后的广告信息
        return advertisementMapper.getAdvertisementById(advertisement.getAdId());
    }

    @Override
    @Transactional
    public boolean updateAdvertisement(AdvertisementDTO advertisementDTO) {
        Advertisement advertisement = new Advertisement();
        BeanUtils.copyProperties(advertisementDTO, advertisement);
        
        // 只保存时间部分，不包含年月日
        if (advertisement.getStartTime() != null) {
            Calendar cal = Calendar.getInstance();
            cal.setTime(advertisement.getStartTime());
            
            // 创建一个只包含时间部分的 java.sql.Time 对象
            java.sql.Time sqlTime = new java.sql.Time(
                cal.get(Calendar.HOUR_OF_DAY), 
                cal.get(Calendar.MINUTE), 
                cal.get(Calendar.SECOND)
            );
            advertisement.setStartTime(sqlTime);
        }
        
        if (advertisement.getEndTime() != null) {
            Calendar cal = Calendar.getInstance();
            cal.setTime(advertisement.getEndTime());
            
            // 创建一个只包含时间部分的 java.sql.Time 对象
            java.sql.Time sqlTime = new java.sql.Time(
                cal.get(Calendar.HOUR_OF_DAY), 
                cal.get(Calendar.MINUTE), 
                cal.get(Calendar.SECOND)
            );
            advertisement.setEndTime(sqlTime);
        }
        
        return advertisementMapper.updateAdvertisement(advertisement) > 0;
    }

    @Override
    @Transactional
    public boolean deleteAdvertisement(Long adId) {
        return advertisementMapper.deleteAdvertisementById(adId) > 0;
    }

    @Override
    public Advertisement getAdvertisementById(Long adId) {
        return advertisementMapper.getAdvertisementById(adId);
    }

    @Override
    public PageInfo<Advertisement> getAllAdvertisements(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Advertisement> advertisements = advertisementMapper.getAllAdvertisements();
        return new PageInfo<>(advertisements);
    }

    @Override
    public List<Advertisement> getActiveAdvertisements() {
        return advertisementMapper.getActiveAdvertisements(new Date());
    }

    @Override
    public List<Advertisement> getActiveAdvertisementsByPosition(Integer positionId) {
        return advertisementMapper.getActiveAdvertisementsByPosition(positionId, new Date());
    }

    @Override
    @Transactional
    public boolean publishAdvertisement(Long adId) {
        return advertisementMapper.updateAdvertisementStatus(adId, 1) > 0;
    }

    @Override
    @Transactional
    public boolean unpublishAdvertisement(Long adId) {
        return advertisementMapper.updateAdvertisementStatus(adId, 2) > 0;
    }

    @Override
    public PageInfo<Advertisement> getAdvertisementList(Advertisement advertisement, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Advertisement> advertisements = advertisementMapper.getAdvertisementList(advertisement);
        return new PageInfo<>(advertisements);
    }

    @Override
    public List<Advertisement> getActiveAdvertisementsByPositionAndTimeSlot(Integer positionId) {
        Date currentTime = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(currentTime);
        int currentHour = calendar.get(Calendar.HOUR_OF_DAY);
        int currentMinute = calendar.get(Calendar.MINUTE);
        
        return advertisementMapper.getActiveAdvertisementsByPositionAndTimeSlot(
            positionId, currentTime, currentHour, currentMinute);
    }
} 